package com.sjsu.CMPE226.etl.utils;

/**
 * This is String to Date COnverter Util.   
 */

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Pawar Deepali
 * 
 */
public class DateUtils {
	private static DateFormat formatter = new SimpleDateFormat("yyyyMMdd/HHmm");

	public static java.sql.Timestamp convertStringToDate(String dateStr) {
		Date convertedDate = null;
		java.sql.Timestamp timeStampDate = null;
		try {
			convertedDate = (Date) formatter.parse(dateStr);
			timeStampDate = new Timestamp(convertedDate.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timeStampDate;
	}

	public static void main(String[] args) {
		System.out.println("date " + convertStringToDate("20120227/0245"));
	}

	public static Date getArchiveDataDate(int xDaysBeforeFrom) {
		Calendar rightNow = getNow();
		rightNow.add(Calendar.DATE, -xDaysBeforeFrom);
		return rightNow.getTime();

	}

	public static Timestamp getDate(int xDaysBefore) {
		Calendar rightNow = getNow();

		rightNow.add(Calendar.DATE, -xDaysBefore);
		java.sql.Timestamp timeStampDate = new Timestamp(rightNow.getTime()
				.getTime());
		return timeStampDate;

	}

	/**
	 * @return
	 */
	public static Calendar getNow() {
		Calendar rightNow = Calendar.getInstance();
		Date now = new Date(System.currentTimeMillis());
		rightNow.setTime(now);
		rightNow.set(Calendar.MINUTE, 0);
		rightNow.set(Calendar.HOUR_OF_DAY, 0);
		rightNow.set(Calendar.SECOND, 1);
		return rightNow;
	}
}
